var curFolder = null;

function toggleChildren(element) {
	$(element).next(".children").toggle();
	$(element).children("#closed").toggle();
	$(element).children("#open").toggle();
	var urls = $(element).next().next().html();
	$("#contents-urls-contents").html(urls);

	if(element != curFolder) {
		$(element).css("background", "#40E0D0");
		if(null != curFolder) {
			$(curFolder).css("background", "");
		}
		curFolder = element;
	}
}

$(document).ready(function() {
	var path = new Array;

	var tabs = new Array;
	tabs.push("\t");

	function parse(rootObject) {
		var txt = "";
		$.each(rootObject.roots, function(i, field) {
			txt += parse_folder(field);
		});
		return txt;
	}

	function parse_array(array) {
		tabs.push("\t");
		var folderChild = "<div class='children subfolder'>{children}</div>";
		var urlChild = "<div class='children urls'>{children}</div>";
		//var curNode = $(txt);
		//var folders = jsonPath(array, "$..[?(@.type == 'folder')]");
		//var urls = jsonPath(array, "$..[?(@.type == 'url')]");
		var folders = [];
		var urls = [];

		for(var i = 0; i < array.length; i++) {
			var obj = array[i];
			if('folder' == array[i].type) {
				folders.push(obj);
			} else if('url' == array[i].type) {
				urls.push(obj);
			}
		};

		var txt = "";
		var html = "";
		for(var i = 0; i < folders.length; i++) {
			html += parse_folder(folders[i]);
		};
		txt += folderChild.replace(/{children}/, html);

		html = "";
		for(var i = 0; i < urls.length; i++) {
			html += parse_object(urls[i]);
		};
		txt += urlChild.replace(/{children}/, html);
		//var txt = "<div class='children subfolder'>{children}</div>";
		//for(var i = 0;i < array.length;i++) {
		////txt += parse_object(array[i],curNode);
		//parse_object(array[i],curNode);
		//};
		//txt += "</div>";
		tabs.pop();
		return txt;
	}

	var folder_template = "\
		<div onclick='toggleChildren(this)'>\
		{{tab}}\
		<img id='closed' src='../images/folder_closed.png' alt=''/>\
		<img id='open' style='display: none;' src='../images/folder_open.png' alt=''/>\
		{{name}}\
		</div>";

	function parse_folder(object) {
		var tab = tabs.join("|---");
		var txt = "<div>";
		if($.type(object.children) == "array") {
			object.tab = tab;
			txt += Mustache.render(folder_template, object);
			path.push(object.name);
			txt += parse_array(object.children);
			path.pop();
		} else {
			console.log("unexcepted");
		}
		txt += "</div>";
		return txt;
	}

	//var url_template = "{{tab}}\
	var url_template = "<div>\
	<img class='favicon' src='http://www.google.cn/favicon.ico' alt=''/>\
	<a href='{{url}}'>\
	{{name}}\
	</a>\
	</div>";

	function parse_object(object, curNode) {
		object.tab = tabs.join("|---");
		return Mustache.render(url_template, object);
	}

	$("#search_field").keydown(function(event) {
		switch(event.keyCode) {
		case 13:
			alert("keydown");
			break;
		default:
			break;
		}
	});

	//TODO:优化；递进过滤，加入新的符号时只从显示出来的项里过滤
	$("#filter_field").keyup(function(event) {
		var filter = $(this).val();
		$("#contents-tags").html(filter);
		var regex = new RegExp(filter,'i');
		$("#contents-urls-contents>div").each(function(i,div) {
			if(-1 == $(div).text().search(regex)){
				$(this).hide();
			}
			else{
				$(this).show();
			}
		});
	});

	// $("#contents-folder").tabs();
	//$("#radio").buttonset();

	$("#mytab a").click(function () {
		//e.preventDefault();
		//$(this).tab('show');
		//alert($(this).text());
	})

	$('#tooltip').tooltip({
		placement:'bottom'
	});

	$('#well a').popover({
		placement:'bottom',
		html:'true'
	});

	$('.dropdown-toggle').dropdown();

	$.getJSON("Bookmarks.txt", function(result) {
		//$("#contents-folder").html(parse(result));
		//$("#contents-folder>#tabs-1").html(parse(result));
		//$(".tab-content #home").html(parse(result));
	});
});
